package com.zmj.mobile.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.framework.exception.CustomException;
import com.zmj.framework.response.Result;
import com.zmj.framework.util.StringUtil;
import com.zmj.mobile.dto.SealApplyDTO;
import com.zmj.mobile.dto.SealApplyVO;
import com.zmj.mobile.entity.SealApplyEntity;
import com.zmj.mobile.enums.ExceptionEnum;
import com.zmj.mobile.mapstruct.SealApplyMapStruct;
import com.zmj.mobile.service.ISealApplyService;
import com.zmj.modules.flow.entity.dto.BpmTaskCcQueryDto;
import com.zmj.modules.flow.entity.vo.TodoTaskVO;
import com.zmj.modules.flow.service.FlowableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 用印申请表 前端控制器
 * </p>
 *
 * @author lzy
 * @since 2024-02-01
 */
@Slf4j
@RestController
@Api(tags = {"SealApply"})
@RequestMapping("/v1/api/seal/apply")
public class SealApplyController {
    @Resource
    private ISealApplyService iSealApplyService;
    @Resource
    private FlowableService flowableService;

    @ApiOperation(value = "新增")
    @PostMapping("/insert")
    public Result<?> insert(@RequestBody @Valid SealApplyDTO insertDTO) {
        iSealApplyService.insert(insertDTO);
        return Result.success();
    }

    /**
     * 查询用章详情
     *
     * @param id 入参
     * @return 出参
     */
    @GetMapping("/getById")
    @ApiOperation(value = "/getById")
    public Result<?> getById(@RequestParam Long id) {

        SealApplyEntity entity = iSealApplyService.detail(id);
        SealApplyVO sealApplyVO = SealApplyMapStruct.INSTANCE.toVO(entity);

        //查询待办
        String flowId = entity.getFlowId();
        if (StringUtils.isEmpty(flowId)) {
            return Result.success(sealApplyVO);
        }
        BpmTaskCcQueryDto bpmQueryDto = new BpmTaskCcQueryDto();
        bpmQueryDto.setProcInstId(flowId);
        bpmQueryDto.setSize(5);
        Result<Map<String, Object>> allTodoTasks = flowableService.getAllTodoTasks(bpmQueryDto);
        Map<String, Object> dataMap = allTodoTasks.getData();
        if (dataMap == null) {
            throw new CustomException(ExceptionEnum.GET_TASK_INFO_FAIL);
        }
        Page<Map<String, Object>> data = BeanUtil.fillBeanWithMap(dataMap, new Page<>(), false);
        List<Map<String, Object>> records = data.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            sealApplyVO.setTaskId("");
            return Result.success(sealApplyVO);
        }
        //if (records.size() > 1) {
        //    //log.warn("业务id#{}#工作流id#{}#询价审批待办任务不唯一！", id, flowId);
        //}
        Map<String, Object> recordMap = records.get(0);
        TodoTaskVO todoTaskVO = BeanUtil.fillBeanWithMap(recordMap, new TodoTaskVO(), false);
        sealApplyVO.setTaskId(todoTaskVO.getTaskId());
        return Result.success(sealApplyVO);
    }


}
